var Branch = {

    selected_row_id: null,
    objGrid: null,

    init: function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url: $('#table-url').val(),
            reload_after_add: true,
            dataType: 'json',
            colModel: [
                {display: 'ID', name: 'id', width: 20, sortable: false, align: 'left', hide: true},
                {display: 'Branches Name', name: 'name', width: 300, sortable: true, align: 'left', hide: false, editable: {form: 'text', validate: 'empty', defval: ''}}
            ],
            formFields: [
                {type: 'buttom', name: 'New', bclass: 'add', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Save', bclass: 'save', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Edit', bclass: 'edit', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Cancel', bclass: 'cancel', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Delete', bclass: 'delete', onpress: self.handleGridEvent},
                {separator: true}
            ],
            searchitems: [
                {display: 'Branches Name', name: 'name', isdefault: true}
            ],
            checkboxSelection: true,
            editablegrid: {
                use: true,
                url: $('#table-editable-url').val()
            },
            columnControl: false,
            sortname: "id",
            sortorder: "desc",
            usepager: true,
            useRp: true,
            rp: 15,
            showTableToggleBtn: false,
            height: 300,
            subgrid: {
                use: false
            }
        });
    },

    handleGridEvent: function (com, grid, json) {
        if (com == 'New') {
            Branch.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            if (FlexObject.rowSelectedCheck(Branch.objGrid, grid)) {
                var row = FlexObject.getSelectedRows(grid);
                Branch.objGrid.flexBeginEdit(row[0]);
            }
        }
        else if (com == 'Save') {
            Branch.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            Branch.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            if (FlexObject.rowSelectedCheck(Branch.objGrid, grid)) {
                Branch.delete_(Branch.objGrid, grid);
            }
        }
    },

    delete_: function (griObject, grid_dom) {
        var self = this;
        var url = $('#table_delete_url').val();
        Jlib.do_delete(griObject, url, grid_dom);
    }
};

/* when the page is loaded */
$(document).ready(function () {
    Branch.init();
});